#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
typedef long long ll;
ll a[N];
int n, k;
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    ll g;
    int x;
    cin >> g >> x;
    a[x + 1] = g;
  }
  for (int i = 1; i < N; ++i) {
    a[i] += a[i - 1];
  }
  ll ans = -1;
  for (int i = 0; i <= N; ++i) {
    int l = max(1, i - k), r = min(N - 1, i + k);
    ans = max(ans, a[r] - a[l - 1]);
  }
  cout << ans << endl;
}
